package com.ebook.server.controller;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.ebook.server.service.UserService;
import com.ebook.util.Status;

/**
 * 用户Controller
 * @author   潘吉友
 * @Date	 2013-2-25
 */
@Controller
@RequestMapping(value = "/user")
public class LoginController {

	@Resource
	private UserService userService;

    /**
     *用户注册
     * @param request 
     * @return ModelAndView
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException 
    */
    @RequestMapping("/regist")
    public ModelAndView regist(@RequestParam("email") String email,@RequestParam("password") String password,@RequestParam("username") String name,@RequestParam("type") String type) throws NoSuchAlgorithmException, UnsupportedEncodingException{  
    	return userService.regist(email,password,name,type);
    }
    
    @RequestMapping("/jumplogin")
    public ModelAndView jumplogin(HttpServletRequest request){
		return new ModelAndView("/user/login");
	}

	/**
	 * 用户登录
	 * @param email
	 * @param password
	 * @param request
	 * @return 信息
	*/
	@RequestMapping("/login")
    public ModelAndView login(@RequestParam("email") String email,@RequestParam("password") String password,@RequestParam("device_id") String device_id,HttpServletRequest request){
		return userService.login(email, password,device_id, request);
	}
	
	
	/**
	 * 修改用户密码
	 * @param request
	 * @return ModelAndView 
	*/
	@RequestMapping("/modifyPsw")
    public ModelAndView modifyPassword(@RequestParam("email") String email,@RequestParam("oldPassword") String oldPassword,@RequestParam("newPassword") String newPassword,HttpServletRequest request){
		return userService.modifyPassword(email,oldPassword,newPassword,request);
	}
	
	/**
	 * 给email地址发邮件
	 * @param email
	 * @param request
	 * @return 成功失败信息
	*/
	@RequestMapping("/sendPsw")
	public ModelAndView sendPswByEmail(@RequestParam("email") String email,HttpServletRequest request) {
		return userService.sendPswByEmail(email);
	}

	
	/**
	 * 核对email和动态码，成功跳转到重置密码页面，失败跳转到错误页面
	 * @param email
	 * @param checkcode
	 * @return 视图
	*/
	@RequestMapping("/editPsw")
	public ModelAndView editPsw(@RequestParam("email") String email,@RequestParam("checkcode") String checkcode,HttpServletRequest request) {
		return userService.editPsw(email, checkcode);
	}

	
	/**
	 * 重置密码功能
	 * @param email
	 * @param password
	 * @param password1
	 * @param request
	 * @return 重置成功转重置成功页面；失败转到重置密码页面
	*/
	@RequestMapping("/resetPsw")
	public ModelAndView resetPsw(@RequestParam("email") String email,@RequestParam("password") String password,@RequestParam("password1") String password1,HttpServletRequest request) {
		return userService.resetPsw(email, password, password1);
	}

	/**
	 * 登录退出
	 * @param request
	 * @return 登录页面
	*/
	@RequestMapping("/loginOut")
    public ModelAndView loginOut(@RequestParam("uid") int uid,@RequestParam("device_id") String deviceId,
    		@RequestParam("CODE") String verificationCode,HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<String, Object>();
		if (!userService.isCertified(uid, verificationCode)) {
			modelMap.put("status", Status.FAIL);
			return new ModelAndView().addAllObjects(modelMap);
		}
		userService.loginOut(uid, deviceId);
		request.getSession().setAttribute("loginuser", null);
		modelMap.put("status", Status.SUCCESS);
        return new ModelAndView().addAllObjects(modelMap);
	}
}
